<template>
  <div class="search-page">
    <div class="container">
      <!-- 搜索信息 -->
      <div class="search-info">
        <h1 class="search-title">
          搜索结果 "<span class="keyword">{{ keyword }}</span>"
        </h1>
        <div class="search-meta">
          <span class="result-count">找到 {{ total }} 件產品</span>
        </div>
      </div>

      <!-- 主要内容区域 -->
      <div class="main-content">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator=">" class="breadcrumb">
          <el-breadcrumb-item to="/">首頁</el-breadcrumb-item>
          <el-breadcrumb-item>搜索结果</el-breadcrumb-item>
          <el-breadcrumb-item>{{ keyword }}</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 產品列表 -->
        <ProductList
            :products="products"
            :loading="loading"
            :total="total"
            @sort-change="handleSortChange"
            @page-change="handlePageChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref, onMounted, computed} from 'vue'
import {useRoute} from 'vue-router'
import ProductList from '@/components/product/ProductList.vue'
import {fetchSearch} from "@/request/req.ts";
import {commonStore} from '@/store/common.ts'

const common = commonStore()
const route = useRoute()

// 搜索相关数据
const keyword = ref('')
const loading = ref(false)
const products = ref<any[]>([])
const total = ref(0)
const serverUrl = computed(() => {
  const item = common.getItemById(1)
  return item?.value
})



// 排序和分页
const currentSort = ref('default')
const sortOrder = ref('asc')
const currentPage = ref(1)
const pageSize = ref(12)

// 从路由获取搜索关键词
const keywordFromRoute = computed(() => route.query.keyword as string || '')
const type = computed(() => route.query.type as string || '')

// 搜索
const performSearch = async () => {
  if (!keywordFromRoute.value.trim()) return

  loading.value = true

  try {
    const res = await fetchSearch({t: type.value, k: keywordFromRoute.value})

    if (res && res.GoodsList) {
      products.value = res.GoodsList.map((item: any) => ({
        id: item.Id,
        name: item.GoodsName,
        price: item.Pirce,
        originalPrice: item.Pirce,
        image: `${serverUrl.value}/images/UpLoadPic/${item.PicUrls}`,
        description: item.ProductDescription,
        rating: item.StarCount / 2,
        reviewCount: item.Views || 0,
        sales: item.SalesVolume || 0,
        tags: [], // 可以根据需要添加标签
        goodsNumber: item.GoodsNumber,
        typeId: item.TypeId,
        addTime: item.AddTime
      }))
      total.value = products.value.length

      // 更新搜索关键词显示
      keyword.value = keywordFromRoute.value
    } else {
      products.value = []
      total.value = 0
    }

  } catch (error) {
    console.error('搜索失败:', error)
    products.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}




// 处理排序变化
const handleSortChange = (sort: string, order: string) => {
  currentSort.value = sort
  sortOrder.value = order
  currentPage.value = 1
  performSearch()
}

// 处理分页变化
const handlePageChange = (page: number, size: number) => {
  currentPage.value = page
  pageSize.value = size
  performSearch()
}

onMounted(() => {
    performSearch()
})
</script>

<style scoped>
.search-page {
  background: var(--bg-tertiary);
  min-height: 100vh;
  padding: var(--spacing-xl) 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 var(--spacing-xl);
}

/* 搜索信息 */
.search-info {
  background: var(--bg-primary);
  padding: var(--spacing-xl);
  border-radius: var(--radius-lg);
  margin-bottom: var(--spacing-xl);
  box-shadow: var(--shadow-card);
  border: 1px solid var(--border-primary);
}

.search-title {
  font-size: var(--font-size-2xl);
  color: var(--text-primary);
  margin-bottom: var(--spacing-md);
}

.keyword {
  color: var(--primary-color);
  font-weight: var(--font-weight-bold);
}

.search-meta {
  display: flex;
  gap: var(--spacing-xl);
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

.result-count {
  font-weight: var(--font-weight-medium);
  color: var(--text-primary);
}

/* 主要内容区域 */
.main-content {
  background: var(--bg-primary);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-card);
  margin-bottom: var(--spacing-4xl);
  border: 1px solid var(--border-primary);
}

.breadcrumb {
  padding: var(--spacing-lg) var(--spacing-xl);
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border-secondary);
}

.breadcrumb :deep(.el-breadcrumb__item) {
  color: var(--text-primary);
}

.breadcrumb :deep(.el-breadcrumb__item .el-breadcrumb__inner) {
  color: var(--text-primary);
  font-weight: var(--font-weight-medium);
  transition: var(--transition-all);
}

.breadcrumb :deep(.el-breadcrumb__item .el-breadcrumb__inner:hover) {
  color: var(--primary-color);
}

.breadcrumb :deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  color: var(--primary-color);
  font-weight: var(--font-weight-semibold);
}

.breadcrumb :deep(.el-breadcrumb__separator) {
  color: var(--text-secondary);
  margin: 0 var(--spacing-sm);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .main-content {
    grid-template-columns: 250px 1fr;
    gap: var(--spacing-lg);
  }

}

@media (max-width: 768px) {
  .main-content {
    grid-template-columns: 1fr;
    gap: var(--spacing-lg);
  }

  .search-info {
    padding: var(--spacing-lg);
  }

  .search-title {
    font-size: var(--font-size-xl);
  }

  .search-meta {
    flex-direction: column;
    gap: var(--spacing-sm);
  }
}

@media (max-width: 480px) {
  .container {
    padding: 0 var(--spacing-md);
  }

}
</style>
